home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / UIFlow 1.0.1 / UIFlow Source / VSet2.0 / Src / vconcat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-20  |  3.9 KB  |  146 lines  |  [TEXT/????]

  1.  
  2. #include "vg.h"
  3.  
  4. main (ac,av) int ac; char**av;
  5. {
  6. char *srcfile, *dstfile;
  7.  
  8. if (ac!=3) {
  9.   fprintf (stderr, "%s: appends one vset file into another\n",av[0]);
  10.   fprintf (stderr, "usage %s srcfile dstfile\n",av[0]);
  11.   exit(0);
  12.   }
  13.   srcfile = av[1];
  14.   dstfile = av[2];
  15.  vappendfile (srcfile, dstfile);
  16. }
  17. /* ------------------------------------------------------------------ */
  18.  
  19. #define VMAXSIZE 12
  20.  
  21. static int nvg = 0, nvs = 0;
  22. static VGROUP *  vgnewptr[VMAXSIZE]; /* new attached vgroups in dst file */
  23. static int vgoldref[VMAXSIZE], vgnewref[VMAXSIZE];
  24. static int vsoldref[VMAXSIZE], vsnewref[VMAXSIZE];
  25.  
  26. /* ------------------------------------------------------------------ */
  27.  
  28. /* 
  29. * Given tag and (old) ref, this function looks in either vgoldref[] or
  30. * vsoldref[] and returns the corrsponding new ref from vgnewref[] or 
  31. * vsnewref[]. 
  32. * Return value is a ref (+ve integer) if found.
  33. * If error or not found, -1 is returned.
  34. */
  35.  
  36. static int lookupnewref ( tag, ref) 
  37. int tag, ref;
  38. {
  39.     int i;
  40.     VGROUP * vg, * vgnew;
  41.  
  42.     if (tag == VSDESCTAG) {
  43.         for(i=0;i<nvs;i++) if (ref == vsoldref[i]) return (vsnewref[i]);
  44.     }
  45.     else if (tag == VGDESCTAG) {
  46.         for(i=0;i<nvg;i++) if (ref == vgoldref[i]) return (vgnewref[i]);
  47.     }
  48.     return (-1);
  49. }
  50.  
  51. /* ------------------------------------------------------------------ */
  52. /* kkk */
  53. vappendfile (srcfile, dstfile) 
  54. char *srcfile, *dstfile; 
  55. {
  56.    DF         *f, *g;
  57.     int         ref, newref;
  58.     DFdesc     desc;
  59.     VGROUP     *vg, *vgnew;
  60.     int        i,j;
  61.     unsigned char* buf;
  62.  
  63.    if (NULL==(f=DFopen(srcfile,DFACC_READ,0))) {
  64.     fprintf(stderr,"cannot open srcfile %s. DFerror=%d\n", srcfile, DFerror);
  65.     exit(0);
  66.     }
  67.    if (NULL==(g=DFopen(dstfile,DFACC_ALL,0))) {
  68.     fprintf(stderr,"cannot open dstfile %s. DFerror=%d\n", dstfile, DFerror);
  69.     exit(0);
  70.     }
  71.  
  72.     /* read in the id's of the vdatas in srcfile */
  73.         ref = -1; 
  74.         while( -1 != (ref = VSgetid (f, ref))) {
  75.             if (nvs>=VMAXSIZE-1) { printf("Vdata work tables full\n"); exit(0);}
  76.             vsoldref[nvs]  = ref;
  77.             nvs++;
  78.         }
  79.  
  80.     /* read in the id's of the vgroups in srcfile */
  81.         ref = -1; 
  82.         while( -1 != (ref = Vgetid (f, ref))) {
  83.             if (nvg>=VMAXSIZE-1) { printf("Vgroup work tables full\n"); exit(0);}
  84.             vgoldref[nvg]  = ref;
  85.             nvg++;
  86.         }
  87.  
  88.     /* ----------- Next, move all vdatas ----------- */
  89.         for(i=0;i<nvs;i++) {
  90.  
  91.             /* create a new id for each vdata in dstfile */
  92.                 ref             = vsoldref[i];
  93.                 newref         = vnewref (g);
  94.                 vsnewref[i] = newref;
  95.  
  96.             /* using that id, copy the vdata data into dstfile */
  97.                 DFsetfind(f, VSDATATAG, ref);
  98.                 DFfind(f, &desc);
  99.                 buf = (unsigned char*) DFIgetspace ( desc.length );
  100.                 if (buf==NULL) { printf("no space for vsdata\n"); exit(0); }
  101.                 DFgetelement (f, VSDATATAG, ref, buf);
  102.                 DFputelement (g, VSDATATAG, newref, buf, desc.length);
  103.                 DFIfreespace (buf);
  104.  
  105.             /* using that id, copy the vdata descriptor into dstfile */
  106.                 DFsetfind(f, VSDESCTAG, ref);
  107.                 DFfind(f, &desc);
  108.                 buf = (unsigned char*) DFIgetspace ( desc.length );
  109.                 if (buf==NULL) { printf("no space for vsdesc\n"); exit(0); }
  110.                 DFgetelement (f, VSDESCTAG, ref, buf);
  111.                 DFputelement (g, VSDESCTAG, newref, buf, desc.length);
  112.                 DFIfreespace (buf);
  113.         }
  114.  
  115.     /* ----------- Finally, move the vgroups ------- */
  116.  
  117.         /* create a new vgroup in dstfile for each vgroup in srcfile */
  118.             for(i=0;i<nvg;i++) {
  119.                 if (NULL == (vg = (VGROUP*) Vattach (g,-1,"w"))) 
  120.                              { printf("vgroup attach error in dstfile\n"); exit(0); }
  121.                 vgnewptr[i]    = vg;
  122.                 vgnewref[i]    = vg->oref;
  123.             }
  124.  
  125.         /* update new vgroups with new refs, and then write out the new vgroups */
  126.             for(i=0;i<nvg;i++) {
  127.                 vgnew = vgnewptr[i];
  128.                 vg = (VGROUP*) Vattach (f, vgoldref[i], "r");
  129.  
  130.                 for (j=0;j<vg->nvelt;j++) {
  131.                     newref = lookupnewref (vg->tag[j], vg->ref[j]);
  132.                     vinsertpair (vgnew, vg->tag[j], newref);
  133.                 }
  134.                 /* === is this all??? more???? === */
  135.                 Vsetname(vgnew, vg->vgname);
  136.                 Vsetclass(vgnew, vg->vgclass);
  137.  
  138.                 Vdetach (vgnew);
  139.                 Vdetach (vg);
  140.             }
  141.  
  142.     /* ----------- all done, close all files ---- */
  143.         DFclose(f);
  144.         DFclose(g);
  145. }
  146.